package com.tulele.vtool.handler;

import com.google.common.collect.Maps;
import com.tulele.vtool.model.combinedEntity.CombinedCustomerInfo;
import com.tulele.vtool.model.combinedEntity.CombinedCustomerYearInfo;
import com.tulele.vtool.model.combinedEntity.LookArticleStatisEntity;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.ArticleTableInfoService;
import com.tulele.vtool.service.IArticleInfoService;
import com.tulele.vtool.service.ILookArticleInfoService;
import com.tulele.vtool.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * 数据模块
 */
@Slf4j
@RestController
public class DataCenterHandler {
    @Autowired
    private ILookArticleInfoService lookArticleInfoService;

    @Autowired
    private ArticleTableInfoService articleTableInfoService;

    @Autowired
    private IArticleInfoService articleInfoService;

    /**
     * 客户资产运营数据 - 概览
     * @param userId 用户 ID
     * @return 概览数据
     */
    @PostMapping(value = "/customerSummary", produces = "application/json;charset=UTF-8")
    public RespData customerSummary(@RequestHeader("userId") String userId) {
        int todayReadCount = 0;// 今日阅读量
        int totalReadCount = 0;// 阅读总量
        int visitorCount = 0;// 访客总量
        int formCount = 0;// 表单总量

        Integer finalUser = Integer.valueOf(userId);

        // 1、今日阅读量
        LookArticleStatisEntity todayLookArticleStatisInfo = lookArticleInfoService.getTodayLookArticleStatisInfo(finalUser);
        if (todayLookArticleStatisInfo != null) {
            todayReadCount = Optional.ofNullable(todayLookArticleStatisInfo.getTodayLookCount()).orElse(0);
        }

        // 2、阅读总量
        LookArticleStatisEntity totalTlookArticleStatisInfo = lookArticleInfoService.getTotalLookArticleStatisInfo(finalUser);
        if (totalTlookArticleStatisInfo != null) {
            totalReadCount = Optional.ofNullable(totalTlookArticleStatisInfo.getTotalLookCount()).orElse(0);
        }

        // 3、访客总量
        visitorCount = lookArticleInfoService.getVisitorCount(finalUser);

        // 4、表单总量
        formCount = articleTableInfoService.getArticleTableCountByOwner(finalUser);

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(4);
        resultMap.put("todayReadCount", todayReadCount);
        resultMap.put("totalReadCount", totalReadCount);
        resultMap.put("visitorCount", visitorCount);
        resultMap.put("formCount", formCount);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }

    /********************************************** 第一部分：阅读数据 **********************************************/

    /**
     * 客户资产运营数据 - 阅读数据 - 本周
     * @param userId 用户 ID
     * @return 本周数据明细
     */
    @PostMapping(value = "/customerCurrentWeekReadDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentWeekReadDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerInfo> currentWeekReadList = lookArticleInfoService.getCurrentWeekReadData(Integer.valueOf(userId));
        if (CollectionUtils.isNotEmpty(currentWeekReadList)) {
            int size = currentWeekReadList.size();
            for (int i = 0; i < size; i++) {
                CombinedCustomerInfo info = currentWeekReadList.get(i);
                info.setWeek(DateUtil.getWeek(i));
            }
        }

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentWeekReadList", currentWeekReadList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 阅读数据 - 本月
     * @param userId 用户 ID
     * @return 本月数据明细
     */
    @PostMapping(value = "/customerCurrentMonthReadDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentMonthReadDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerInfo> currentMonthReadList = lookArticleInfoService.getCurrentMonthReadData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentMonthReadList", currentMonthReadList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 阅读数据 - 本季
     * @param userId 用户 ID
     * @return 本季数据明细
     */
    @PostMapping(value = "/customerCurrentSeasonReadDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentSeasonReadDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerYearInfo> currentSeasonReadList = lookArticleInfoService.getCurrentSeasonReadData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentSeasonReadList", currentSeasonReadList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 阅读数据 - 年度
     * @param userId 用户 ID
     * @return 年度数据明细
     */
    @PostMapping(value = "/customerCurrentYearReadDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentYearReadDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerYearInfo> currentYearReadList = lookArticleInfoService.getCurrentYearReadData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentYearReadList", currentYearReadList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /********************************************** 第二部分：发布数据 **********************************************/

    /**
     * 客户资产运营数据 - 发布数据 - 本周
     * @param userId 用户 ID
     * @return 本周数据明细
     */
    @PostMapping(value = "/customerCurrentWeekPublishDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentWeekPublishDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerInfo> currentWeekPublishList = articleInfoService.getCurrentWeekPublishData(Integer.valueOf(userId));
        if (CollectionUtils.isNotEmpty(currentWeekPublishList)) {
            int size = currentWeekPublishList.size();
            for (int i = 0; i < size; i++) {
                CombinedCustomerInfo info = currentWeekPublishList.get(i);
                info.setWeek(DateUtil.getWeek(i));
            }
        }

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentWeekPublishList", currentWeekPublishList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 发布数据 - 本月
     * @param userId 用户 ID
     * @return 本月数据明细
     */
    @PostMapping(value = "/customerCurrentMonthPublishDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentMonthPublishDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerInfo> currentMonthPublishList = articleInfoService.getCurrentMonthPublishData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentMonthPublishList", currentMonthPublishList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 发布数据 - 本季
     * @param userId 用户 ID
     * @return 本季数据明细
     */
    @PostMapping(value = "/customerCurrentPublishReadDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentPublishReadDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerYearInfo> currentSeasonPublishList = articleInfoService.getCurrentSeasonPublishData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentSeasonPublishList", currentSeasonPublishList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 发布数据 - 年度
     * @param userId 用户 ID
     * @return 年度数据明细
     */
    @PostMapping(value = "/customerCurrentYearPublishDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentYearPublishDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerYearInfo> currentYearPublishList = articleInfoService.getCurrentYearPublishData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentYearPublishList", currentYearPublishList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /********************************************** 第三部分：意向数据 **********************************************/

    /**
     * 客户资产运营数据 - 意向数据 - 本周
     * @param userId 用户 ID
     * @return 本周数据明细
     */
    @PostMapping(value = "/customerCurrentWeekIntentionDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentWeekIntentionDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerInfo> currentWeekIntentionList = lookArticleInfoService.getCurrentWeekIntentionData(Integer.valueOf(userId));
        if (CollectionUtils.isNotEmpty(currentWeekIntentionList)) {
            int size = currentWeekIntentionList.size();
            for (int i = 0; i < size; i++) {
                CombinedCustomerInfo info = currentWeekIntentionList.get(i);
                info.setWeek(DateUtil.getWeek(i));
            }
        }

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentWeekIntentionList", currentWeekIntentionList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 意向数据 - 本月
     * @param userId 用户 ID
     * @return 本月数据明细
     */
    @PostMapping(value = "/customerCurrentMonthIntentionDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentMonthIntentionDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerInfo> currentMonthIntentionList = lookArticleInfoService.getCurrentMonthIntentionData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentMonthIntentionList", currentMonthIntentionList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 意向数据 - 本季
     * @param userId 用户 ID
     * @return 本季数据明细
     */
    @PostMapping(value = "/customerCurrentSeasonIntentionDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentSeasonIntentionDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerYearInfo> currentSeasonIntentionList = lookArticleInfoService.getCurrentSeasonIntentionData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentSeasonIntentionList", currentSeasonIntentionList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 意向数据 - 年度
     * @param userId 用户 ID
     * @return 年度数据明细
     */
    @PostMapping(value = "/customerCurrentYearIntentionDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentYearIntentionDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerYearInfo> currentYearIntentionList = lookArticleInfoService.getCurrentYearIntentionData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentYearIntentionList", currentYearIntentionList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /********************************************** 第三部分：表单数据 **********************************************/

    /**
     * 客户资产运营数据 - 表单数据 - 本周
     * @param userId 用户 ID
     * @return 本周数据明细
     */
    @PostMapping(value = "/customerCurrentWeekTableDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentWeekTableDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerInfo> currentWeekTableList = articleTableInfoService.getCurrentWeekTableData(Integer.valueOf(userId));
        if (CollectionUtils.isNotEmpty(currentWeekTableList)) {
            int size = currentWeekTableList.size();
            for (int i = 0; i < size; i++) {
                CombinedCustomerInfo info = currentWeekTableList.get(i);
                info.setWeek(DateUtil.getWeek(i));
            }
        }

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentWeekTableList", currentWeekTableList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 表单数据 - 本月
     * @param userId 用户 ID
     * @return 本月数据明细
     */
    @PostMapping(value = "/customerCurrentMonthTableDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentMonthTableDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerInfo> currentMonthTableList = articleTableInfoService.getCurrentMonthTableData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentMonthTableList", currentMonthTableList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 表单数据 - 本季
     * @param userId 用户 ID
     * @return 本季数据明细
     */
    @PostMapping(value = "/customerCurrentSeasonTableDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentSeasonTableDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerYearInfo> currentSeasonTableList = articleTableInfoService.getCurrentSeasonTableData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentSeasonTableList", currentSeasonTableList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }

    /**
     * 客户资产运营数据 - 表单数据 - 年度
     * @param userId 用户 ID
     * @return 年度数据明细
     */
    @PostMapping(value = "/customerCurrentYearTableDetail", produces = "application/json;charset=UTF-8")
    public RespData customerCurrentYearTableDetail(@RequestHeader("userId") String userId) {
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        List<CombinedCustomerYearInfo> currentYearTableList = articleTableInfoService.getCurrentYearTableData(Integer.valueOf(userId));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("currentYearTableList", currentYearTableList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        if (resultMap.size() > 0) {
            respData.setData(resultMap);
        }
        return respData;
    }
}
